// 通用的应用代码
import { createSSRApp } from "vue";
import App from "./App.vue";
import { createSSRRouter } from "./router";
import ElementPlus, { ElMessage } from "element-plus";
import "element-plus/dist/index.css";
import { createSSRI18n } from "./language/i18n";
import { createSSRStore, key } from "./store";
import { sync } from "vuex-router-sync"; // 将路由信息同步到Vuex中

export function createApp() {
  const app = createSSRApp(App);
  const router = createSSRRouter();
  const store = createSSRStore();
  const i18n = createSSRI18n();
  sync(store, router);

  // app全局变量
  app.config.globalProperties.$message = ElMessage;

  // app注册
  app.use(store, key);
  app.use(router);
  app.use(ElementPlus);
  app.use(i18n);

  return { app, router, store };
}

// 从客户端和服务端抽离出来的方法
export function asyncDataFilter(actived: any, store: any, route: any) {
  // eslint-disable-next-line array-callback-return
  return Promise.all(actived.map((Component: any) => {
    if (Component.asyncData) {
      return Component.asyncData({
        store,
        route
      });
    }
  }));
}
